//
//  MeatOrderListCell.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/26.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatOrderListCell.h"

@interface MeatOrderListCell ()<UITextFieldDelegate>


/**
 店铺名
 */
@property (nonatomic, strong) UILabel *shopNameLab;


/**
 数量
 */
@property (nonatomic, strong) UILabel *numberLab;


/**
 配送方式
 */
@property (nonatomic, strong) UILabel *sendStyleLab;



/**
 快递 免费
 */
@property (nonatomic, strong) UILabel *freeSendLab;



/**
 订单备注
 */
@property (nonatomic, strong) UILabel *orderNoteLab;


/**
 猪类型
 */
@property (nonatomic, strong) UILabel *pigTypeTipLab;


/**
 猪编号
 */
@property (nonatomic, strong) UILabel *pigTypeNOLab;


/**
 猪重量
 */
@property (nonatomic, strong) UILabel *pigTypeWeightLab;


/**
 猪单价
 */
@property (nonatomic, strong) UILabel *pigTypePriceSignalLab;


/**
 价格
 */
@property (nonatomic, strong) UILabel *priceLab;


/**
 arrowBtn
 */
@property (nonatomic, strong) UIButton *arrowBtn;


/**
 icons
 */
@property (nonatomic, strong) NSMutableArray *icons;


@property (nonatomic, strong) NSMutableArray *images;



@end


@implementation MeatOrderListCell

#pragma mark - lifecycle methods

- (void)setupUI{
    
   _type = MeatOrderListCellType_More;
    
    [self addSubview:self.shopNameLab];
    [self addSubview:self.numberLab];
    [self addSubview:self.sendStyleLab];
    [self addSubview:self.freeSendLab];
    [self addSubview:self.orderNoteLab];
    
    [self addSubview:self.pigTypeTipLab];
    [self addSubview:self.pigTypeNOLab];
    [self addSubview:self.pigTypeWeightLab];
    [self addSubview:self.pigTypePriceSignalLab];
    [self addSubview:self.priceLab];
    
    [self addSubview:self.textField];
    [self addSubview:self.arrowBtn];
    
    
    [self.icons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIImageView *icon = (UIImageView *)obj;
        [self addSubview:icon];
    }];

}

- (void)setupLayout{
    
    CGFloat width = KScreenWidth - 30;
    CGFloat height = 183;
    
    switch (self.type) {
        case MeatOrderListCellType_More:
            {
                @weakify(self);
                [self.icons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    @strongify(self);
                    
                    __block UIImageView *icon = (UIImageView *)obj;
                    icon.hidden = YES;
                    
                    [self.images enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger index, BOOL * _Nonnull stop) {
                        
                        if (idx == index){
                            icon.hidden = NO;
                            [icon sd_setImageWithURL:[NSURL URLWithString:obj]];
                        }
                    }];
                }];
                
                self.pigTypeTipLab.hidden = YES;
                self.pigTypeNOLab.hidden = YES;
                self.pigTypeWeightLab.hidden = YES;
                self.pigTypePriceSignalLab.hidden = YES;
                self.priceLab.hidden = YES;
                self.arrowBtn.hidden = NO;
                self.numberLab.hidden = NO;
            }
            break;
            
        default:
            {
                @weakify(self);
                [self.icons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    @strongify(self);
                    UIImageView *icon = (UIImageView *)obj;
                    icon.hidden = idx == 0 ?NO :YES;
                    if (idx == 0) {
                        Meats *meats = self.item.meets[0];
                        MeatsItem *meatItem = meats.list[0];
                        [icon sd_setImageWithURL:[NSURL URLWithString:meatItem.headImg]];
                        
                        self.pigTypeTipLab.text = meatItem.aliasName;
                        self.pigTypeNOLab.text = meatItem.chunksName;
                        self.pigTypeWeightLab.text = [NSString stringWithFormat:@"重量%@kg",meatItem.weight];
                        self.pigTypePriceSignalLab.text = [NSString stringWithFormat:@"单价%@元/500g", meatItem.unitPrice];
                        self.priceLab.text = [NSString stringWithFormat:@"¥%@",meatItem.nowPrice];
                    }
                }];
                
                self.pigTypeTipLab.hidden = NO;
                self.pigTypeNOLab.hidden = NO;
                self.pigTypeWeightLab.hidden = NO;
                self.pigTypePriceSignalLab.hidden = NO;
                self.priceLab.hidden = NO;
                self.numberLab.hidden = YES;
                self.arrowBtn.hidden = YES;

            }
            break;
    }
    
    
    {
        _shopNameLab.frame = CGRectMake(15, 15, width -30 , 21);
    }
    
    {
        _numberLab.size = CGSizeMake(50, 40);
        _numberLab.mj_y = 61;
        _numberLab.mj_x = width - 21 - _numberLab.width;
    }
    
    
    {
        _sendStyleLab.size = CGSizeMake(60, 18);
        _sendStyleLab.mj_x = 15;
        _sendStyleLab.mj_y = _shopNameLab.bottom + 86;
    }
    
    {
        _freeSendLab.size = CGSizeMake(80, 18);
        _freeSendLab.mj_x = width - _freeSendLab.width - 15;;
        _freeSendLab.mj_y = _shopNameLab.bottom + 86;

    }
    
    {
        _orderNoteLab.size = CGSizeMake(60, 18);
        _orderNoteLab.mj_x = 15;
        _orderNoteLab.mj_y = _sendStyleLab.bottom + 10;
    }
    
    
    {
        _textField.size = CGSizeMake(width - 84, 18);
        _textField.mj_x = _orderNoteLab.right + 12;;
        _textField.mj_y = _orderNoteLab.mj_y ;
    }
    
    
    {
        _arrowBtn.size = CGSizeMake(40, 40);
        _arrowBtn.mj_y = 61;
        _arrowBtn.mj_x = width - _arrowBtn.width ;
    }
    
    //////////////////////////////////////////////
    
    float gap = 5;
    {
        _pigTypeTipLab.frame = CGRectMake(0, 0, KScreenWidth, 20);
        [_pigTypeTipLab sizeToFit];
        _pigTypeTipLab.width += 20;
        _pigTypeTipLab.height += gap ;
        _pigTypeTipLab.mj_x = 83;
        _pigTypeTipLab.mj_y = 53;
    }
    
    {
        _pigTypeNOLab.size = CGSizeMake(136, 19);
        _pigTypeNOLab.mj_x = _pigTypeTipLab.right + 5;
        _pigTypeNOLab.mj_y = 53;
    }
    
    
    {
        _pigTypeWeightLab.frame = CGRectMake(0, 0, KScreenWidth, 30);
        [_pigTypeWeightLab sizeToFit];
        _pigTypeWeightLab.mj_x = 83;
        _pigTypeWeightLab.width += 20;
        _pigTypeWeightLab.height += gap;
        _pigTypeWeightLab.mj_y = _pigTypeNOLab.bottom + 5;;
    }
    
    
    {
        _pigTypePriceSignalLab.frame = CGRectMake(0, 0, KScreenWidth, 30);
        [_pigTypePriceSignalLab sizeToFit];
        _pigTypePriceSignalLab.width += 20;
        _pigTypePriceSignalLab.height += gap;
        _pigTypePriceSignalLab.mj_x = _pigTypeWeightLab.right + 5;
        _pigTypePriceSignalLab.mj_y = _pigTypeNOLab.bottom + 5;
    }
    
    {
        _priceLab.size = CGSizeMake(87, 18);
        _priceLab.mj_x = width - _priceLab.width - 15;
        _priceLab.mj_y = (height - _priceLab.height)/2.0;
    }
}



#pragma mark - delegate methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (self.editBlock) {
        self.editBlock(0);
    }
    return YES;
}

#pragma mark - setter methods


- (void)setItem:(MeatPayOrderItem *)item{
    _item = item;
    
    NSString *newStr = [NSString stringWithFormat:@"%@%@",@" ", item.shopName];
    NSMutableAttributedString *aims = [newStr createAttributingByIcon:@"meat_found_shopIcon" downIndexImage:@"meat_cart_enter" textColor:UIColorFromRGB(0x333333) font:mMediumFont(17)];
    _shopNameLab.attributedText = aims;
    
    if (self.images.count >0) {
        [self.images removeAllObjects];
    }
    
    __block NSInteger total = 0;
    @weakify(self);
    [item.meets enumerateObjectsUsingBlock:^(Meats * _Nonnull meats, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        
        total += meats.list.count;
        [meats.list enumerateObjectsUsingBlock:^(MeatsItem * _Nonnull obj, NSUInteger index, BOOL * _Nonnull stop) {
            [self.images addObject:obj.headImg];
        }];
    }];
    
    _numberLab.text = [NSString stringWithFormat:@"共%@件", @(total)];
    self.type = total >1 ?MeatOrderListCellType_More :MeatOrderListCellType_One;
    
    [self setupLayout];
}


#pragma mark - getter methods

- (UILabel *)shopNameLab{
    if (!_shopNameLab) {
        _shopNameLab = [[UILabel alloc] init];
    }
    return _shopNameLab;
}


- (UILabel *)numberLab{
    
    if (!_numberLab) {
        _numberLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x999999) font:mFont(12) textAliment:2];
    }
    return _numberLab;
}


- (UILabel *)sendStyleLab{
    
    if (!_sendStyleLab) {
        _sendStyleLab = [UILabel makeLabelWithText:@"配送方式" textColor:UIColorFromRGB(0x454545) font:mFont(13) textAliment:0];
    }
    return _sendStyleLab;
}


- (UILabel *)freeSendLab{
    
    if (!_freeSendLab) {
        _freeSendLab = [UILabel makeLabelWithText:@"快递 免费" textColor:UIColorFromRGB(0x454545) font:mFont(13) textAliment:2];
    }
    return _freeSendLab;
}


- (UILabel *)orderNoteLab{
    
    if (!_orderNoteLab) {
        _orderNoteLab = [UILabel makeLabelWithText:@"订单备注" textColor:UIColorFromRGB(0x454545) font:mFont(13) textAliment:0];
    }
    return _orderNoteLab;
}



- (UILabel *)pigTypeTipLab{
    
    if (! _pigTypeTipLab){
        _pigTypeTipLab = [UILabel makeLabelWithText:@"" textColor:mMainColor font:mFont(12) textAliment:1];
        _pigTypeTipLab.backgroundColor = UIColorFromRGB(0xE9F9F0);
        [self addShadowAndCircleCorner:_pigTypeTipLab.layer corner:3];
    }
    return _pigTypeTipLab;
}


- (UILabel *)pigTypeNOLab{
    
    if (!_pigTypeNOLab){
        _pigTypeNOLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x333333) font:mFont(15) textAliment:0];
    }
    return _pigTypeNOLab;
}

- (UILabel *)pigTypeWeightLab {
    
    if (!_pigTypeWeightLab){
        _pigTypeWeightLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x9C9C9C) font:mFont(10) textAliment:1];
        _pigTypeWeightLab.backgroundColor = UIColorFromRGB(0xF2F4F7);
        [self addShadowAndCircleCorner:_pigTypeWeightLab.layer corner:9];
    }
    return _pigTypeWeightLab;
}


- (UILabel *)pigTypePriceSignalLab{
    
    if (!_pigTypePriceSignalLab){
        _pigTypePriceSignalLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x9C9C9C) font:mFont(10) textAliment:1];
        _pigTypePriceSignalLab.backgroundColor = UIColorFromRGB(0xF2F4F7);
        [self addShadowAndCircleCorner:_pigTypePriceSignalLab.layer corner:9];
    }
    return _pigTypePriceSignalLab;
}


- (UILabel *)priceLab{
    
    if (!_priceLab) {
        _priceLab = [UILabel makeLabelWithText:@"¥ 172.8" textColor:UIColorFromRGB(0x4D4D4D) font:mMediumFont(12) textAliment:2];
    }
    return _priceLab;
}

- (UIButton *)arrowBtn{
    
    if (!_arrowBtn) {
        _arrowBtn = [UIButton makeButtonWithImageName:@"meat_cart_enter"];
        _arrowBtn.contentMode = UIControlContentHorizontalAlignmentRight;
        
    }
    return _arrowBtn;
}


- (UITextField *)textField
{
    if (!_textField) {
        
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(34, 0, KScreenWidth-15-36-80, 36)];
        _textField.placeholder = @"可以给商家留言";
        _textField.delegate = self;
        _textField.textColor = UIColorFromRGB(0x454545);
        _textField.font = mFont(14);
        
//        kWeakSelf(self);
//        _textField.tapBlock = ^{
//
//        };
    }
    return _textField;
}

- (NSMutableArray *)icons{
    
    if (!_icons) {
        _icons = [NSMutableArray array];
       
        for (NSInteger i = 0; i< 4; i++) {
            
            UIImageView *icon = [UIImageView makeImageByImageName:@"" cornerRadius:8];
            icon.tag = 500 + i;
            icon.size = CGSizeMake(56, 56);
            icon.mj_x = 12 + (56 + 8)*i;
            icon.mj_y = 51;
            [_icons addObject:icon];
        }
    }
    return _icons;
}


- (NSMutableArray *)images{
    
    if (!_images) {
        _images = [NSMutableArray array];
    }
    return _images;
}


@end
